<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input clearable @keyup.enter.native="handleFilter" style="width: 150px;" class="filter-item" placeholder="请输入卡号" v-model="listQuery.cardNo">
      </el-input>
      <el-select clearable v-model="listQuery.stateCode" placeholder="巡检状态">
        <el-option label="待巡检" value="0"></el-option>
        <el-option label="已巡检" value="1"></el-option>
        <el-option label="已拒绝" value="2"></el-option>
      </el-select>
      <el-date-picker
        v-model="value"
        type="datetimerange"
        align="right"
        clearable
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        format="yyyy-MM-dd HH:mm:ss"
        value-format="yyyy-MM-dd HH:mm:ss"
        @change="getTime"
        :default-time="['12:00:00', '08:00:00']">
      </el-date-picker>
      <el-button size="small" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='设备名称' width="150">
        <template slot-scope="scope">
          {{scope.row.deviceName}}
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.deviceTypeName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="卡号" align="center">
        <template slot-scope="scope">
          {{scope.row.cardNo}}
        </template>
      </el-table-column>
      <el-table-column label="垃圾类型" align="center" width="120">
        <template slot-scope="scope">
          {{scope.row.typeName}}
        </template>
      </el-table-column>
      <el-table-column label="巡检状态" align="center" width="100">
        <template slot-scope="scope">
          {{scope.row.stateName}}
        </template>
      </el-table-column>
      <el-table-column label="投递重量（g）" align="center" width="120">
        <template slot-scope="scope">
          {{scope.row.weight}}
        </template>
      </el-table-column>
      <el-table-column label="投递时间" align="center" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.stateCode == 0" type="primary" size="mini" @click="bianji(scope.row,scope.$index)" plain>巡检</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--编辑表单弹窗-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="formsrules" ref="dataForm" :model="temp" label-position="right" label-width="120px" style='margin-left:20px; margin-right: 20px;'>
        <el-form-item label="位置：">
          <el-select v-model="temp.stateCode" placeholder="请选择">
            <el-option label="待巡检" :value="0"></el-option>
            <el-option label="通过" :value="1"></el-option>
            <el-option label="不通过" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="temp.stateCode==2" label="拒绝原因：">
          <el-input type="textarea" v-model="temp.refusedReason"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="addrole">确 定</el-button>
        <el-button v-else type="primary" @click="sureEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--编辑表单弹窗END-->

    <!--分页-->
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30,50]"
                     :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
    <!--分页END-->
  </div>
</template>

<script>
  import { getList, sureEdit } from '@/api/device/deviceputInList'
  import { mapGetters } from 'vuex'

  export default {
    data() {
      return {
        list: null,
        listLoading: true,
        total: null,
        listQuery: {
          barrelId: this.$route.params.uuid,
          cardNo: '',
          stateCode: '',
          timeStart: '',
          timeEnd: ''
        },
        temp: {
          uuid: undefined,
          stateCode: '',
          refusedReason: ''
        },
        dialogStatus: '',
        typeList: undefined,
        value: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        dialogFormVisible: false,
        formsrules: {
          name: [{ required: true, message: '请输入', trigger: 'blur' }]
        }
      }
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          normal: '正常',
          disble: '停止'
        }
        return statusMap[status]
      }
    },
    computed: {
      ...mapGetters([
        'token'
      ])
    },
    created() {
      this.fetchData()
    },
    methods: {
      // 获取列表
      fetchData() {
        this.listLoading = true
        getList(this.listQuery).then(response => {
          this.list = response.data.content
          this.total = response.data.totalElements
          this.listQuery.size = response.data.size
          this.listLoading = false
        })
      },
      handleFilter() {
        this.fetchData()
      },
      getTime(date) {
        this.listQuery.timeStart = date[0]
        this.listQuery.timeEnd = date[1]
      },
      bianji(row) {
        this.temp = Object.assign({}, row)
        this.temp.recommend = row.recommend
        this.fileList = [{ name: '', url: this.temp.img }]
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      sureEdit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.temp.stateCode === 0) {
              this.temp.stateName = '待巡检'
            } else if (this.temp.stateCode === 1) {
              this.temp.stateName = '已巡检'
            } else {
              this.temp.stateName = '已拒绝'
            }
            this.temp.token = this.token
            const temData = Object.assign({}, this.temp)
            sureEdit(temData).then(response => {
              for (const v of this.list) {
                if (v.id === this.temp.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.temp)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          } else {
            return false
          }
        })
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.fetchData()
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .filter-container{
    padding-bottom: 10px;
  }
</style>
